home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Interfaces / Universal Interfaces 2.0a3 / CIncludes / fstream.h < prev    next >
Encoding:
Text File  |  1994-12-14  |  9.5 KB  |  362 lines  |  [TEXT/MPS ]

  1. //     IOStreams Package
  2. //     Steve Teale April 1992
  3. //     Copyright Symantec Corp 1990-1992. All Rights Reserved.
  4. //
  5. //    Some portions Copyright © 1994 Apple Computer Inc.
  6.  
  7.  
  8. #ifndef __FSTREAM_H
  9. #define __FSTREAM_H
  10.  
  11. #include <iostream.h>
  12.  
  13. class filebuf : public streambuf {
  14.  
  15. // This is a streambuf class specialized for handling files.
  16. // The get and put pointers are locked together so that reads and writes
  17. // happen at the same offset into the file.
  18.  
  19. public:
  20.     enum { openprot = 0644 };
  21.  
  22.     filebuf();
  23. // The default constructor. Creats a filebuf that is not associated
  24. // with any file. open() or attach() can then be used to connect
  25. // to a file.
  26.  
  27.     filebuf(int file_descriptor, int io_mode = ios::in|ios::out
  28. #if M_UNIX || M_XENIX
  29.             );
  30. #else
  31.             |ios::translated);
  32. #endif
  33. // Constructs a filebuf for the open file attached to the argument
  34. // file descriptor.     More comprehensive io_mode information can
  35. // be specified e.g. ios::app, if required
  36.  
  37.     filebuf(int descriptor, char *memory, int length,
  38.                             int io_mode = ios::in|ios::out
  39. #if M_UNIX || M_XENIX
  40.             );
  41. #else
  42.             |ios::translated);
  43. #endif
  44. // Constructs a filebuf for the open file attached to the
  45. // file_descriptor, and sets the buffer to "memory", which is of
  46. // "length" bytes in size. If memory is 0 or length is <= 0,
  47. // it is taken as a request that the file be unbuffered.
  48.  
  49.     ~filebuf();
  50.  
  51.     filebuf *attach(int file_descriptor,
  52.                             int io_mode = ios::in|ios::out
  53. #if M_UNIX_ || M_XENIX
  54.             );
  55. #else
  56.             |ios::translated);
  57. #endif
  58. // Attaches an open file to a filebuf. Returns "this" if successful,
  59. // 0 if the filebuf is already attached to a file.
  60.  
  61.     filebuf *close();
  62. // Flushes output, closes the file, and detaches the file from this
  63. // filebuf. Clears the error state unless there is an error flushing
  64. // the output. Will always close the file and detach it from the
  65. // filebuf, even if there are errors.
  66.  
  67.     int fd() const { return file; };
  68. // Returns the file descriptor for the connected file. If the
  69. // filebuf is closed or not attached to a file, returns EOF.
  70.  
  71.     int is_open() const { return file != EOF; };
  72. // Returns non-zero when this filebuf is attached to a file,
  73. // otherwise returns zero.
  74.  
  75.     filebuf *open(const char *name, int io_mode,
  76.                     int protection = openprot);
  77. // Opens the file "name", and connects it to this filebuf.
  78. // io_mode is a bit-mask containing one or more of the values of
  79. // enum open_mode:
  80. // ios::in       Open for reading.
  81. // ios::out       Open for writing.
  82. // ios::ate       Position to the end-of-file.
  83. // ios::app       Open the file in append mode.
  84. // ios::trunc  Truncate the file on open.
  85. // ios::nocreate   Do not attempt to create the file if it
  86. //               does not exist.
  87. // ios::noreplace  Cause the open to fail if the file exists.
  88. // ios::translate  Convert CR/LF to newline on input and
  89. //               vice versa on output
  90.  
  91.     streampos seekoff(streamoff offset, ios::relative_to,
  92.                                 int which);
  93. // Relative seek the get and put pointers within the file.
  94. // The get and put pointers of a filebuf always point to the
  95. // same byte of the file.
  96.  
  97.     streambuf *setbuf(char *memory, int length);
  98. // Set the buffer to use "memory", of "length" bytes.
  99. // If memory == 0 or length <= 0, it is taken as a request that
  100. // I/O to the file be unbuffered.
  101.  
  102.     int sync();
  103. // Flush any bytes in the get buffer, and re-position the file so
  104. // that is appears they were never read. Write any bytes in the
  105. // put buffer to the file.
  106.  
  107. #if __ZTC__ > 0x214
  108.     int overflow(int c);
  109. #else
  110.     int overflow(int c = EOF);
  111. #endif
  112. // Flush bytes in the put area to the file.
  113.  
  114.     int underflow();
  115. // Get more bytes for reading.
  116.  
  117. protected:
  118.     int doallocate();
  119.  
  120.     int pbackfail(int c);
  121. // Called to atempt recovery if putback attempted at
  122. // start of get buffer
  123.  
  124. private:
  125.  
  126.     void buffer_setup();
  127.                 // Internal. Set up I/O buffer.
  128.     int newlines();
  129.                 // count newline chars in the get buffer
  130.     int syncin();
  131.     int syncout();
  132.                 // two halves of sync() function
  133.     int fillbuf();
  134.     int flushbuf();
  135.                 // Functions which actually transfer to/from
  136.                 // the file
  137.  
  138.     int file;    // File descriptor for the associated file.
  139.     short mode; // I/O mode from the argument to open().
  140.    char unbuf[2];
  141.                // pseudo buffer for unbuffered operation
  142.    char *gptr_;
  143.    char *egptr_;
  144.                // Save old gptr() & egptr() while using the
  145.                // pushback buffer.
  146.    char pushback_buf[4];
  147.                // Reserve buffer for pushback.
  148.                // Only used if there is no room for pushback in
  149.                // the regular buffer.
  150.     char do_not_seek;
  151.                 // Set if the file (device) does not support seeks.
  152.                 // This is set for a TTY, or a Unix pipe.
  153.     char own_file_descriptor;
  154.                 // Set if the file descriptor is from open, and
  155.                 // the file should be closed by the destructor.
  156.     static const int lseek_consts[3];
  157.                 // A look up table for the lseek constants from
  158.                 // the appropriate C header file
  159. };
  160.  
  161. class fstream_common : virtual public ios {
  162.  
  163. // Features common to ifstream, ofstream, and fstream.
  164.  
  165. public:
  166.  
  167.     void attach(int file_descriptor, int io_mode);
  168. // Attach the filebuf to the argument file descriptor, error state
  169. // set to ios::failbit|ios::badbit on failure.
  170.  
  171.     void close();
  172. // Flush the filebuf, and close the file attached to it. Error state
  173. // set ios::failbit|ios::badbit if rdbuf()->sync() fails. File closed
  174. // regardless.
  175.  
  176.     void open(const char *name, int io_mode,
  177.                 int protection = filebuf::openprot);
  178. // Open a file, and attach it to the filebuf. Error state set to
  179. // ios::failbit|ios::badbit on failure
  180.  
  181.     void setbuf(char *memory, int length)
  182.     {
  183.         buffer.setbuf(memory, length);
  184.     }
  185. // Use the argument memory, of the given length, as the I/O buffer.
  186.     filebuf *rdbuf() { return &buffer; }
  187. // Note that fstream_common::rdbuf returns a filebuf*
  188. // instead of a streambuf*.
  189.  
  190. protected:
  191.     fstream_common();
  192.     filebuf buffer;
  193. };
  194.  
  195. class ifstream : public fstream_common, public istream {
  196. public:
  197.     ifstream();
  198. // Create an ifstream not attached to any file.
  199.  
  200.     ifstream(const char *name, int io_mode = ios::in | ios::nocreate
  201. #if M_UNIX || M_XENIX
  202.         ,int protection = filebuf::openprot);
  203. #else
  204.         | ios::translated, int protection = filebuf::openprot);
  205. #endif
  206. // Open the argument file and create an ifstream attached to it.
  207.                 
  208.     ifstream(int file_descriptor, int io_mode = ios::in
  209. #if M_UNIX || M_XENIX
  210.         );
  211. #else
  212.         | ios::translated);
  213. #endif
  214. // Create an ifstream attached to the argument file descriptor.
  215.  
  216.     ifstream(int file_descriptor, char *memory, int length,
  217.                                             int io_mode = ios::in
  218. #if M_UNIX || M_XENIX
  219.         );
  220. #else
  221.         | ios::translated);
  222. #endif
  223. // Create an ifstream attached to the argument file descriptor, and
  224. // using the argument memory as the I/O buffer.
  225.  
  226.     ~ifstream();
  227.  
  228.     void attach(int file_descriptor, int io_mode = ios::in
  229. #if M_UNIX || M_XENIX
  230.         )
  231. #else
  232.         | ios::translated)
  233. #endif
  234.     {
  235.         fstream_common::attach(file_descriptor, io_mode);
  236.     }
  237.  
  238.     void open(const char *name, int io_mode = ios::in
  239. #if M_UNIX || M_XENIX
  240.         ,int protection = filebuf::openprot)
  241. #else
  242.         | ios::translated, int protection = filebuf::openprot)
  243. #endif
  244.     {
  245.         fstream_common::open(name, io_mode, protection);
  246.     }
  247.  
  248. };
  249.  
  250. class ofstream : public fstream_common, public ostream {
  251. public:
  252.     ofstream();
  253. // Create an ofstream not attached to any file.
  254.  
  255.     ofstream(const char *name, int io_mode = ios::out
  256. #if M_UNIX || M_XENIX
  257.         ,int protection = filebuf::openprot);
  258. #else
  259.         | ios::translated, int protection = filebuf::openprot);
  260. #endif
  261. // Open the argument file and create an ofstream attached to it.
  262.                 
  263.     ofstream(int file_descriptor, int io_mode = ios::out
  264. #if M_UNIX || M_XENIX
  265.         );
  266. #else
  267.         | ios::translated);
  268. #endif
  269. // Create an ofstream attached to the argument file descriptor.
  270.  
  271.     ofstream(int file_descriptor, char *memory, int length,
  272.                                     int io_mode = ios::out
  273. #if M_UNIX || M_XENIX
  274.         );
  275. #else
  276.         | ios::translated);
  277. #endif
  278. // Create an ofstream attached to the argument file descriptor, and
  279. // using the argument memory as the I/O buffer.
  280.  
  281.     ~ofstream();
  282.  
  283.     void attach(int file_descriptor, int io_mode = ios::out
  284. #if M_UNIX || M_XENIX
  285.         )
  286. #else
  287.         | ios::translated)
  288. #endif
  289.     {
  290.         fstream_common::attach(file_descriptor, io_mode);
  291.     }
  292.  
  293.     void open(const char *name, int io_mode = ios::out
  294. #if M_UNIX_ || M_XENIX
  295.         ,int protection = filebuf::openprot)
  296. #else
  297.         | ios::translated, int protection = filebuf::openprot)
  298. #endif
  299.     {
  300.         fstream_common::open(name, io_mode, protection);
  301.     }
  302.  
  303. };
  304.  
  305.  
  306. class fstream : public fstream_common, public iostream {
  307. public:
  308.     fstream();
  309. // Create an fstream not attached to any file.
  310.  
  311.     fstream(const char *name, int io_mode = ios::in|ios::out
  312. #if M_UNIX || M_XENIX
  313.         ,int protection = filebuf::openprot);
  314. #else
  315.         | ios::translated, int protection = filebuf::openprot);
  316. #endif
  317. // Open the argument file and create an fstream attached to it.
  318.                 
  319.     fstream(int file_descriptor, int io_mode = ios::in | ios::out
  320. #if M_UNIX || M_XENIX
  321.         );
  322. #else
  323.         | ios::translated);
  324. #endif
  325. // Create an fstream attached to the argument file descriptor.
  326.  
  327.     fstream(int file_descriptor, char *memory, int length,
  328.                                         int io_mode = ios::in | ios::out
  329. #if M_UNIX || M_XENIX
  330.         );
  331. #else
  332.         | ios::translated);
  333. #endif
  334. // Create an fstream attached to the argument file descriptor, and
  335. // using the argument memory as the I/O buffer.
  336.  
  337.     ~fstream();
  338.  
  339.     void attach(int file_descriptor, int io_mode = ios::in | ios::out
  340. #if M_UNIX || M_XENIX
  341.         )
  342. #else
  343.         | ios::translated)
  344. #endif
  345.     {
  346.         fstream_common::attach(file_descriptor, io_mode);
  347.     }
  348.  
  349.     void open(const char *name, int io_mode = ios::in | ios::out
  350. #if M_UNIX || M_XENIX
  351.         ,int protection = filebuf::openprot)
  352. #else
  353.         | ios::translated, int protection = filebuf::openprot)
  354. #endif
  355.     {
  356.         fstream_common::open(name, io_mode, protection);
  357.     }
  358.  
  359. };
  360.  
  361. #endif    // __FSTREAM_H
  362.